package com.example.iotest.bio;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;


public class Test2 {
    static final Logger logger= LoggerFactory.getLogger(Test2.class);
    public static void main(String[] args) {
        /*ByteArrayInputStream inputStream=new ByteArrayInputStream(new byte[]{1,2,3});
        int read = inputStream.read();
        System.out.println(inputStream.read());
        System.out.println(inputStream.read());
        System.out.println(inputStream.read());*/
        try (FileInputStream fileInputStream=new FileInputStream("D:\\Resources\\GitProjects\\io-test\\io-test\\io-test\\src\\main\\resources\\files\\1.txt")){
            byte[] buffer = new byte[3];
            int read = fileInputStream.read(buffer);
            System.out.println(new String(buffer,0,3));
            Thread.sleep(5000);
            System.out.println(111);
            int read2 = fileInputStream.read(buffer);
            System.out.println(new String(buffer,0,3));
        }catch (Exception e){
            logger.error("错误",e);
        }
    }

    @Test
    public void f1() {
        try (FileInputStream fileInputStream=new FileInputStream("target/classes/files/1.txt")){
            byte[] buffer = new byte[2];
            System.out.println(fileInputStream.available());
            fileInputStream.read(buffer);
            System.out.println(fileInputStream.available());
            for (byte b :
                    buffer) {
                System.out.println(b);
            }
            System.out.println(new String(buffer));
            Thread.sleep(2000);
            System.out.println(new String(buffer,0,2));
            fileInputStream.read();
            System.out.println(fileInputStream.available());
            System.out.println(new String(buffer,0,1));
        }catch (Exception e){
            logger.error("错误",e);
        }
        System.out.println(Charset.defaultCharset());
    }


    //ByteArray
    @Test
    public void f2(){
        try(ByteArrayInputStream inputStream=new ByteArrayInputStream(new byte[]{125,23,11})) {
            int available = inputStream.available();
            System.out.println(available);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try(ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream()) {

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void f3() throws IOException, InterruptedException {
        FileInputStream fileInputStream=new FileInputStream("D:\\Resources\\GitProjects\\io-test\\io-test\\io-test\\src\\main\\resources\\files\\1.txt");
        FileOutputStream fileOutputStream=new FileOutputStream("D:\\Resources\\GitProjects\\io-test\\io-test\\io-test\\src\\main\\resources\\files\\2.txt");
//        byte[] bytes = "111111111111111sss\nssssss11111111111111".getBytes(StandardCharsets.UTF_8);
//        byte[] buffer=new byte[8];
//        fileOutputStream.write(buffer);
//        fileOutputStream.write("\n".getBytes());
//        fileOutputStream.write("\n".getBytes());
//        fileOutputStream.write("\n".getBytes());
//        fileOutputStream.write(buffer);
//        fileOutputStream.write(buffer);
//        fileOutputStream.write(buffer);
//        fileOutputStream.write(buffer);
//        fileOutputStream.write(97);
        byte[] buffer=new byte[8];
        int length;
        while ((length=fileInputStream.read(buffer))!=-1){
            fileOutputStream.write(buffer,0,length);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }
}

